package org.cttic.visualization.visualization.z_service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.cttic.visualization.visualization.common.module.provinceData;
import org.cttic.visualization.visualization.common.module.stationCountData;
import org.cttic.visualization.visualization.common.module.stationCountData.*;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.cttic.visualization.visualization.util.sixUtil.stationCountUtil;
import org.cttic.visualization.visualization.z_service.stationCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.cttic.visualization.visualization.common.module.provinceData.province;

/**
 * @author shkstart
 * @create 2020-05-22 9:58
 */
@Service
public class stationCountImpl implements stationCount {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private stationCountUtil stationCountUtil;
    @Override
    public String queryStationCount(String inputData) {
        JSONObject jsonData = ParseJsonDataUtil.getJsonData(inputData);
        String queryType = jsonData.getString("queryType");
        String provinceId = jsonData.getString("provinceId");
        stationCountData stationCountData = new stationCountData();
        List<vehArr> list = new ArrayList<>();
        //按全国查
        if(provinceId==null){
            //查询方式 1-时间段查询  0-时间点查询
            if(queryType.equals("1")){
                String queryValue1 = jsonData.getString("queryValue1");
                String queryValue2 = jsonData.getString("queryValue2");
                String startTime = DateUtil.TtoTime(queryValue1);
                String endTime = DateUtil.TtoTime(queryValue2);

                Long total=0L;
                for (Integer provincel : provinceData.provinceList) {
                    long value = stationCountUtil.stationCountQueryUtil(startTime, endTime, provincel + "", list);
                    total+=value;

                }
                stationCountData.setTotalCounts(total+"");
                stationCountData.setTotalRatios("98.21%");
                stationCountData.setVehArray(list);
                ObjectMapper mapper = new ObjectMapper();
                try {
                    String value = mapper.writeValueAsString(stationCountData);
                    return value;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }else if(queryType.equals("0")){
                //按时间点查询

            }


        }else if (!provinceId.contains("|")){
            if(queryType.equals("1")){
                //单省查询
                String queryValue1 = jsonData.getString("queryValue1");
                String queryValue2 = jsonData.getString("queryValue2");
                String startTime = DateUtil.TtoTime(queryValue1);
                String endTime = DateUtil.TtoTime(queryValue2);
                long count = stationCountUtil.stationCountQueryUtil(startTime, endTime, provinceId, list);
                stationCountData.setTotalCounts(count+"");
                stationCountData.setTotalRatios("98.21%");
                stationCountData.setVehArray(list);
                ObjectMapper mapper = new ObjectMapper();
                try {
                    String value = mapper.writeValueAsString(stationCountData);
                    return value;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }else if(queryType.equals("0")){
                //按时间点查询

            }
        }else if (provinceId.contains("|")){
            if(queryType.equals("1")){
                //多省查询
                long total=0L;
                String queryValue1 = jsonData.getString("queryValue1");
                String queryValue2 = jsonData.getString("queryValue2");
                String startTime = DateUtil.TtoTime(queryValue1);
                String endTime = DateUtil.TtoTime(queryValue2);
                String[] split = provinceId.split("\\|");
                if(split.length>0&&split!=null) {
                    for (String province : split) {
                        long count = stationCountUtil.stationCountQueryUtil(startTime, endTime, province, list);
                        total += count;

                    }

                    stationCountData.setTotalCounts(total + "");
                    stationCountData.setTotalRatios("98.21%");
                    stationCountData.setVehArray(list);
                    ObjectMapper mapper = new ObjectMapper();
                    try {
                        String value = mapper.writeValueAsString(stationCountData);
                        return value;
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            }else if(queryType.equals("0")){
                //按时间点查询

            }
        }


        return null;
    }
}
